//
//  ECMemberAddViewController.m
//  ECERP.CRM
//
//  Created by 刘思麟 on 2018/11/3.
//  Copyright © 2018 Mac. All rights reserved.
//

#import "ECMemberAddViewController.h"
#import "ECMemberAddTableViewCell.h"
#import "ECButton.h"
#import "ECMemberCommonSearchViewController.h"
#import "ECDatePicker.h"

@interface ECMemberAddViewController ()<UITableViewDelegate,UITableViewDataSource> {
    NSInteger sectionShowSum;
    NSArray * sectionTitles;
    ECDatePicker * datePicker;
}

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) NSArray<NSArray<NSMutableDictionary*>*> * dataArr;

@property (nonatomic,strong) NSMutableDictionary<NSIndexPath*,NSDictionary<NSString*,id>*> * nextSelctDic;

@property (nonatomic,strong) ECButton * showMoreBT;

@end

@implementation ECMemberAddViewController

- (void)viewDidLoad {
    self.navigationItem.title = @"新增客户";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:nil];
    [super viewDidLoad];
    self.tableView.backgroundColor = ECCOLOR(0xF4F4F4);
    // Do any additional setup after loading the view.
}

- (void)loadSubView {
    sectionShowSum = 2;
    sectionTitles = @[@"基本信息",@"客户标签",@"更多联系方式",@"相关人员",@"客户来源记录",@"客户信用",@" "];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(0);
    }];
}

- (void)showMorrInfo:(UIButton*)sender {
    sectionShowSum = self.dataArr.count;
    [self.tableView reloadData];
    self.tableView.tableFooterView.frame = CGRectMake(0, 0, 0, 0.0001);
    self.tableView.tableFooterView.hidden = YES;
}

#pragma mark -UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == sectionTitles.count - 1) {
        return 10;
    }
    return 27;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return sectionTitles[section];
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    UITableViewHeaderFooterView * v = view;
    v.backgroundView.backgroundColor = ECCOLOR(0xF4F4F4);
    v.textLabel.textColor = ECCOLOR(0xB3B3B3);
    v.textLabel.font = ECFONT(13 * PX_RELATIVE375);
    v.left = 18 * PX_RELATIVE375;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return sectionShowSum;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr[section].count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44 * PX_RELATIVE375;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ECMemberAddTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSMutableDictionary * dic = self.dataArr[indexPath.section][indexPath.row];
    cell.isNeed = [[dic objectForKey:@"isNeed"] boolValue];
    cell.cellStyle = [[dic objectForKey:@"style"] integerValue];
    cell.title = [dic objectForKey:@"title"];
    switch (cell.cellStyle) {
        case ECMemberAddCellStyleTxt:
            {
                if (!dic[@"value"] || ((NSString*)dic[@"value"]).length < 1) {
                    cell.detailTF.placeholder = dic[@"placeholder"];
                }else{
                    cell.detailTF.text = dic[@"value"];
                }
            }
            break;
        case ECMemberAddCellStyleMore: {
            if (!dic[@"value"] || ((NSString*)dic[@"value"]).length < 1) {
                cell.detailLB.text = dic[@"placeholder"];
                cell.detailLB.textColor = ECCOLOR(0xcccccc);
            }else{
                cell.detailLB.text = dic[@"value"];
                cell.detailLB.textColor = ECCOLOR(0x333333);
            }
        }
            break;
        case ECMemberAddCellStyleSwitch:{
            if ([dic[@"value"] boolValue]) {
                cell.isON = YES;
            }else {
                cell.isON = NO;
            }
        }
            break;
        default:
            break;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary * dic = [self.nextSelctDic objectForKey:indexPath];
    if (dic) {
        ECMemberCommonSearchViewController * vc = [ECMemberCommonSearchViewController new];
        ECMemberCommonSearchViewModel * vModel = [ECMemberCommonSearchViewModel new];
        vModel.url = dic[@"listUrl"];
        vc.vModel = vModel;
        vc.propsDic = dic;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    NSMutableDictionary * cuDic = self.dataArr[indexPath.section][indexPath.row];
    WS(weakCudic, cuDic);
    WS(weakSelf, self);
    if ([cuDic[@"title"] containsString:@"日期"]) {
        if (!datePicker) {
            datePicker = [ECDatePicker new];
            datePicker.selectedDateBlock = ^(NSDate * _Nonnull date) {
                NSDateFormatter * formatter = [NSDateFormatter new];
                [formatter setDateFormat:@"yyyy-MM-dd"];
                weakCudic[@"value"] = [formatter stringFromDate:date];
                [weakSelf.tableView beginUpdates];
                [weakSelf.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationNone];
                [weakSelf.tableView endUpdates];
            };
        }
        [datePicker show];
    }
}

#pragma mark -lazyLoad

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_WIDTH, 40 * PX_RELATIVE375)];
        [footer addSubview:self.showMoreBT];
        __weak typeof(footer) weakFooter = footer;
        [self.showMoreBT mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(weakFooter);
        }];
        _tableView.tableFooterView = footer;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.00000001)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionHeaderHeight = 27 * PX_RELATIVE375;
        _tableView.sectionFooterHeight = 0.00001 * PX_RELATIVE375;
        [_tableView registerClass:[ECMemberAddTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (ECButton *)showMoreBT {
    if (!_showMoreBT) {
        _showMoreBT = [ECButton new];
        _showMoreBT.splitBetweenImgAndTitle = 0 * PX_RELATIVE375;
        _showMoreBT.imgLocation = ECButonImageLocationRight;
        [_showMoreBT setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.001]];
        [_showMoreBT setTitle:@"扩展信息" forState:UIControlStateNormal];
        _showMoreBT.titleLabel.font = ECFONT(15*PX_RELATIVE375);
        [_showMoreBT setTitleColor:ECCOLOR(0x41D95A) forState:UIControlStateNormal];
        [_showMoreBT setImage:[UIImage imageNamed:@"moreInfo_member"] forState:UIControlStateNormal];
        [_showMoreBT addTarget:self action:@selector(showMorrInfo:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _showMoreBT;
}

- (NSArray<NSArray<NSDictionary *> *> *)dataArr {
    if (!_dataArr) {
        _dataArr = @[
  @[@{@"title":@"客户名称 *",@"isNeed":@(YES),@"placeholder":@"请输入客户名称",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
    @{@"title":@"客户编码 *",@"isNeed":@(YES),@"placeholder":@"请输入客户编码",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
    @{@"title":@"手机号码 *",@"isNeed":@(YES),@"placeholder":@"请输入手机号码",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
    @{@"title":@"详细地址 *",@"isNeed":@(YES),@"placeholder":@"请输入详细地址",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy],
  
  @[@{@"title":@"存储位 *",@"isNeed":@(YES),@"placeholder":@"请选择存储位",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
    @{@"title":@"价格组 *",@"isNeed":@(YES),@"placeholder":@"请选择价格组",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
    @{@"title":@"客户类型 *",@"isNeed":@(YES),@"placeholder":@"请选择客户类型",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
    @{@"title":@"客户来源 *",@"isNeed":@(YES),@"placeholder":@"请选择客户来源",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
    @{@"title":@"状态",@"isNeed":@(NO),@"placeholder":@"正常",@"style":@(ECMemberAddCellStyleSwitch),@"value":@(YES)}.mutableCopy],
  
  @[@{@"title":@"电话号码",@"isNeed":@(NO),@"placeholder":@"请输入手机号码",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
    @{@"title":@"传真",@"isNeed":@(NO),@"placeholder":@"请输入传真号码",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
    @{@"title":@"企业微信号",@"isNeed":@(NO),@"placeholder":@"请输入企业微信号",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
    @{@"title":@"企业网站",@"isNeed":@(NO),@"placeholder":@"请输入企业网站",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy],
  
  @[@{@"title":@"联系人",@"isNeed":@(NO),@"placeholder":@"请输入联系人",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
    @{@"title":@"客户经理",@"isNeed":@(NO),@"placeholder":@"请选择客户经理",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
    @{@"title":@"发货负责人",@"isNeed":@(NO),@"placeholder":@"请选择发货负责人",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy],
  
  @[@{@"title":@"开始合作日期",@"isNeed":@(NO),@"placeholder":@"请选择开始合作日期",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
    @{@"title":@"父级",@"isNeed":@(NO),@"placeholder":@"请选择父级",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
    @{@"title":@"物流公司",@"isNeed":@(NO),@"placeholder":@"请选择物流公司",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy],
  
  @[@{@"title":@"初期欠款",@"isNeed":@(NO),@"placeholder":@"请输入初期欠款",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
    @{@"title":@"授信额度",@"isNeed":@(NO),@"placeholder":@"请输入授信额度",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
    @{@"title":@"星级",@"isNeed":@(NO),@"placeholder":@"请选择星级",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
    @{@"title":@"排序码",@"isNeed":@(NO),@"placeholder":@"请输入排序码",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy],
  
  @[@{@"title":@"备注",@"isNeed":@(NO),@"placeholder":@"请输入备注信息",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy]];
    }
    return _dataArr;
}

- (NSMutableDictionary<NSIndexPath *,NSDictionary<NSString *,id> *> *)nextSelctDic {
    if (!_nextSelctDic) {
        _nextSelctDic = @{
                          [NSIndexPath indexPathForRow:0 inSection:1]:
                              @{@"title":@"选择存储位",@"addTitle":@"新增存储位",@"listUrl":Api_MemberStorePlaceList,@"requestType":@"GET",@"props":@[
                                    @{@"title":@"存储位 *",@"isNeed":@(YES),@"placeholder":@"请输入存储位",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                    @{@"title":@"备注",@"isNeed":@(NO),@"placeholder":@"请输入备注",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                    @{@"title":@"状态",@"isNeed":@(NO),@"placeholder":@"正常",@"style":@(ECMemberAddCellStyleSwitch),@"value":@(YES)}.mutableCopy
                                    ]},
                          [NSIndexPath indexPathForRow:1 inSection:1]:
                              @{@"title":@"选择存价格组",@"addTitle":@"新增价格组",@"listUrl":Api_MemberCustomerLevelList,@"requestType":@"GET",@"props":@[
                                        @{@"title":@"价格组名称 *",@"isNeed":@(YES),@"placeholder":@"请输入价格组名称",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"备注",@"isNeed":@(NO),@"placeholder":@"请输入备注",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"状态",@"isNeed":@(NO),@"placeholder":@"正常",@"style":@(ECMemberAddCellStyleSwitch),@"value":@(YES)}.mutableCopy
                                        ]},
                          [NSIndexPath indexPathForRow:2 inSection:1]:
                              @{@"title":@"选择存客户类型",@"addTitle":@"新增客户类型",@"listUrl":Api_MemberMemberTypeList,@"requestType":@"GET",@"props":@[
                                        @{@"title":@"客户类型 *",@"isNeed":@(YES),@"placeholder":@"请输入客户类型名称",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"备注",@"isNeed":@(NO),@"placeholder":@"请输入备注",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"状态",@"isNeed":@(NO),@"placeholder":@"正常",@"style":@(ECMemberAddCellStyleSwitch),@"value":@(YES)}.mutableCopy
                                        ]},
                          [NSIndexPath indexPathForRow:3 inSection:1]:
                              @{@"title":@"选择客户来源",@"addTitle":@"新增客户来源",@"listUrl":Api_MemberMemberSourceList,@"requestType":@"GET",@"props":@[
                                        @{@"title":@"客户来源 *",@"isNeed":@(YES),@"placeholder":@"请输入客户来源名称",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"备注",@"isNeed":@(NO),@"placeholder":@"请输入备注",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"状态",@"isNeed":@(NO),@"placeholder":@"正常",@"style":@(ECMemberAddCellStyleSwitch),@"value":@(YES)}.mutableCopy
                                        ]},
                          [NSIndexPath indexPathForRow:1 inSection:3]:
                              @{@"title":@"选择客户经理",@"addTitle":@"新增客户经理",@"props":@[
                                        @{@"title":@"姓名 *",@"isNeed":@(YES),@"placeholder":@"请输入姓名",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"性别",@"isNeed":@(NO),@"placeholder":@"请选择性别",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
                                        @{@"title":@"公司",@"isNeed":@(NO),@"placeholder":@"请输入公司名称",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"部门",@"isNeed":@(NO),@"placeholder":@"请输入所在部门",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"电话号码",@"isNeed":@(NO),@"placeholder":@"请输入电话号码",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"手机号码",@"isNeed":@(NO),@"placeholder":@"请输入手机号码",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"备注",@"isNeed":@(NO),@"placeholder":@"请输入备注",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"状态",@"isNeed":@(NO),@"placeholder":@"正常",@"style":@(ECMemberAddCellStyleSwitch),@"value":@(YES)}.mutableCopy
                                        ]},
                          [NSIndexPath indexPathForRow:2 inSection:3]:
                              @{@"title":@"选择发货负责人",@"addTitle":@"新增发货负责人",@"props":@[
                                        @{@"title":@"姓名 *",@"isNeed":@(YES),@"placeholder":@"请输入姓名",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"性别",@"isNeed":@(NO),@"placeholder":@"请选择性别",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
                                        @{@"title":@"公司",@"isNeed":@(NO),@"placeholder":@"请输入公司名称",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"部门",@"isNeed":@(NO),@"placeholder":@"请输入所在部门",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"电话号码",@"isNeed":@(NO),@"placeholder":@"请输入电话号码",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"手机号码",@"isNeed":@(NO),@"placeholder":@"请输入手机号码",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"备注",@"isNeed":@(NO),@"placeholder":@"请输入备注",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"状态",@"isNeed":@(NO),@"placeholder":@"正常",@"style":@(ECMemberAddCellStyleSwitch),@"value":@(YES)}.mutableCopy
                                        ]},
                          [NSIndexPath indexPathForRow:1 inSection:4]:@{@"title":@"选择父级"},
                          [NSIndexPath indexPathForRow:2 inSection:4]:
                              @{@"title":@"选择物流公司",@"addTitle":@"新增物流公司",@"props":@[
                                        @{@"title":@"物流公司 *",@"isNeed":@(YES),@"placeholder":@"请输入物流公司名称",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"备注",@"isNeed":@(NO),@"placeholder":@"请输入备注",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                                        @{@"title":@"状态",@"isNeed":@(NO),@"placeholder":@"正常",@"style":@(ECMemberAddCellStyleSwitch),@"value":@(YES)}.mutableCopy
                                        ]},}.mutableCopy;
    }
    return _nextSelctDic;
}

@end
